package com.ants.config.listener;

import cn.dev33.satoken.listener.SaTokenListener;
import cn.dev33.satoken.session.SaSessionCustomUtil;
import cn.dev33.satoken.stp.SaLoginModel;
import com.ants.common.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * TODO     自定义侦听器的实现
 * Author Chen
 * Date   2021/7/14 10:12
 */
@Component
public class MySaTokenListener implements SaTokenListener {
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 每次登录时触发
     */
    //@Override
    //public void doLogin(String loginType, Object loginId, SaLoginModel loginModel) {
    //    // ...
    //    System.out.println("doLogin");
    //}
    @Override
    public void doLogin(String loginType, Object loginId, String tokenValue, SaLoginModel saLoginModel) {
        System.out.println("doLogin");
    }

    /**
     * 每次注销时触发
     */
    @Override
    public void doLogout(String loginType, Object loginId, String tokenValue) {
        // ...
        System.out.println("doLogout");
        redisUtil.del("onLine:" + tokenValue);
        SaSessionCustomUtil.deleteSessionById(String.valueOf(loginId));
    }

    /**
     * 每次被踢下线时触发
     */
    @Override
    public void doKickout(String s, Object o, String s1) {
        redisUtil.del("onLine:" + o);
        SaSessionCustomUtil.deleteSessionById(String.valueOf(o));
    }

    /**
     * 每次被顶下线时触发
     */
    @Override
    public void doReplaced(String s, Object o, String s1) {
        redisUtil.del("onLine:" + o);
        SaSessionCustomUtil.deleteSessionById(String.valueOf(o));
    }

    /**
     * 每次被封禁时触发
     */
    public void doDisable(String loginType, Object loginId, String service, int level, long disableTime) {

    }

    /**
     * 每次被解封时触发
     */
    @Override
    public void doUntieDisable(String loginType, Object loginId, String service) {

    }

    @Override
    public void doOpenSafe(String loginType, String tokenValue, String service, long safeTime) {

    }

    @Override
    public void doCloseSafe(String loginType, String tokenValue, String service) {

    }

    /**
     * 每次创建Session时触发
     */
    @Override
    public void doCreateSession(String id) {
        // ...
        System.out.println("doCreateSession");
    }

    /**
     * 每次注销Session时触发
     */
    @Override
    public void doLogoutSession(String id) {
        // ...
        System.out.println("doLogoutSession");
    }

    /**
     * TODO     每次Token续期时触发
     *
     * @Date 2023/3/9 13:55
     * @Author chenshuang
     * @Param tokenValue
     * @Param loginId
     * @Param timeout
     * @Return
     */
    @Override
    public void doRenewTimeout(String tokenValue, Object loginId, long timeout) {

    }

}

